<?php
/**
 * @author Robert Hahn
 * @version 02/21/2011
 */
class SnapfacebookHelper extends AppHelper	{
	
	var $helpers = array("Javascript", "Html", "Form", 'Session');

	
	private function display()	{
		return defined("FACEBOOK_APP_ID") && defined("FACEBOOK_SECRET_ID");
	}
	
	function facebookLoginButton($options = array())	{
		if (!$this->display()) return "";
		if (!isset($options['redirect'])) $options['redirect'] = array();
		if (!isset($options['caption'])) $options['caption'] = 'Login with facebook';
		if (!isset($options['rights'])) $options['rights'] = 'email';
		
		$rights = $options['rights'];
		if (is_array($rights)) $rights = implode(',', $rights);
		
		$redirect = $options['redirect'];
		$redirect = "http://".$_SERVER['HTTP_HOST'].$this->url($redirect);
		
		$caption = $options['caption'];
		
		$url = "https://graph.facebook.com/oauth/authorize?client_id=".FACEBOOK_APP_ID."&return_session=1&scope=$rights&redirect_uri=$redirect";
		
		unset($options['caption']);
		unset($options['redirect']);
		unset($options['rights']);
		print $this->Html->link($caption, $url);
	}
	
}